package com.malljelly.study.social.component;

import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.social.security.SocialUser;
import org.springframework.social.security.SocialUserDetails;
import org.springframework.social.security.SocialUserDetailsService;
import org.springframework.stereotype.Component;

/**
 * Created by 骑着蜗牛去旅行 on 2018/8/23.
 */
@Component
@Slf4j
public class MyUserDetailsService implements UserDetailsService ,SocialUserDetailsService {
    //    这个UserDetails 就是封装了用户信息的对象，里面包含了七个方法
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        log.info("登录用户名:"+username);

        return buildUser(username);
    }

    @Override
    public SocialUserDetails loadUserByUserId(String userId) throws UsernameNotFoundException {
        log.info("社交登录用户id:"+userId);

        return buildUser(userId);
    }


    private SocialUserDetails buildUser(String userId) {
        //根据查找到的用户信息判断用户是否被冻结
        String password = new BCryptPasswordEncoder().encode("123456");
        log.info("数据库密码是:"+password);

        // //封装用户信息，并返回。参数分别是：用户名，密码，用户权限
        return new SocialUser(userId, password,
                true, true, true, true,
                AuthorityUtils.commaSeparatedStringToAuthorityList("admin"));
    }
}
